<?php
namespace app\index\controller;
use app\common\controller\Frontend;
use app\common\model\JqPrice;

use app\common\model\HfPrice;
use app\common\model\HfStyle;
use app\common\model\HfType;

use app\common\model\HbPrice;
use app\common\model\HbType;

use app\common\model\Express;
use app\common\model\Shortcut;

use think\Cookie;
class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        if($this->request->isPost()){
            $data = $this->request->post();
            switch ($data['type']) {
                case '1': //锦旗定制
                    $price = JqPrice::where('specs_id',$data['specs']['id'])->where('style_id',$data['style']['id'])->value('price');
                    if($price == null){
                        $this->error('锦旗定制的规格样式不存在');
                    }
                    $remark = Shortcut::where('id',1)->value('remark');
                    if($remark == null){
                        $remark = '锦旗定制快捷语还没设置';
                    }
                    $result = "尺寸：".$data['specs']['name']."\n字体：".$data['style']['name']."\n".round($price+$data['express']['price'],2)."元\n快递：".$data['express']['name']."\n";
                    $remark_arr = explode('\n',$remark);
                    $reamrk_str = "";
                    foreach ($remark_arr as $key => $value) {
                        $reamrk_str.=$value."\n";
                    }
                    $this->success($result.$reamrk_str);
                    break;
                case '2': //横幅定制
                    /*$price1 = HfPrice::where('height',$data['height']<2?2:$data['height'])->where('width',$data['width'])->where('type_id',2)->value('price');
                    if($price1 == null){
                        $this->error('横幅定制尺寸:长'.$data['height'].'米宽'.$data['width'].'米的规格不存在');
                    }*/
                    $width = "";
                    if($data['width'] == "0.5"){
                        $width="0.6";
                    }
                    if($data['width'] == "0.6"){
                        $width="0.6";
                    }
                    if($data['width'] == "0.7"){
                        $width="0.7";
                    }
                    if($data['width'] == "0.8"){
                        $width="0.9";
                    }
                    if($data['width'] == "0.9"){
                        $width="0.9";
                    }
                    $price2 = HfPrice::where('height',$data['height']<2?2:$data['height'])->where('width',$width)->where('type_id',1)->value('price');
                    if($price2 == null){
                        $price2 = "激光布：没有此宽度的规格\n";
                    }else{
                        $price2 = '激光布：'.round(($data['style']['price']*$data['count'])+($price2*$data['count'])+$data['express']['price'],2)."元\n";
                    }
                    $remark = Shortcut::where('id',2)->value('remark');
                    if($remark == null){
                        $remark = '横幅定制快捷语还没设置';
                    }
                    $price = model('HfType')->where('id',2)->value('price');
                    $result = "尺寸：长".$data['height']."米 宽"
                    .$data['width']."米 ".$data['count']."条\n旗帜布："
                    .round(($data['style']['price']*$data['count'])+($price*$data['height']*$data['width']*$data['count'])+$data['express']['price'],2)."元\n".$price2;
                    //."固定方式：".$data['style']['name']."\n快递：".$data['express']['name']."\n";
                    $remark_arr = explode('\n',$remark);
                    $reamrk_str = "";
                    foreach ($remark_arr as $key => $value) {
                        $reamrk_str.=$value."\n";
                    }
                    $this->success($result.$reamrk_str);
                    break;
                case '3': //海报定制
                    
                    /*$price = HbPrice::where('height',$data['height']<2?2:$data['height'])->where('width',$data['width'])->where('type_id',$data['quality']['id'])->value('price');
                    if($price == null){
                        $this->error('海报定制材质'.$data['quality']['name'].'长'.$data['height'].'米宽'.$data['width'].'米的规格不存在');
                    }*/
                    $height = $data['height']<2?2:$data['height'];
                    $remark = Shortcut::where('id',3)->value('remark');
                    if($remark == null){
                        $remark = '海报定制快捷语还没设置';
                    }
                    $result = "尺寸:长".$data['height']."米 宽"
                    .$data['width']."米\n材质:".$data['quality']['name']."\n".round(($data['quality']['price']*$height*$data['width']*$data['count'])+$data['express']['price'],2)."元\n快递：".$data['express']['name']."\n";
                    $remark_arr = explode('\n',$remark);
                    $reamrk_str = "";
                    foreach ($remark_arr as $key => $value) {
                        $reamrk_str.=$value."\n";
                    }
                    $this->success($result.$reamrk_str);
                    break;
            }
        }
        $sign = 0;
        if(Cookie::has('loginToken')){
            $loginToken = Cookie::get('loginToken');
            $token = Shortcut::where('id',4)->value('remark');
            if(md5(md5($token.'hfdz9527')) == $loginToken){
                $sign = 1;
            }
        }
        $this->assign('hbType',json_encode(model('HbType')->where('status',1)->field('id,name,price')->select()));
        $this->assign('hfStyle',json_encode(model('HfStyle')->where('status',1)->order('weigh desc')->field('id,name,price')->select()));
        $this->assign('jqSpecs',json_encode(model('JqSpecs')->where('status',1)->field('id,name')->select()));
        $this->assign('jqStyle',json_encode(model('JqStyle')->where('status',1)->field('id,name')->select()));
        $this->assign('express',json_encode(model('Express')->where('status',1)->field('id,name,price')->select()));
        $this->assign('id',1);
        $this->assign('sign',$sign);
        return $this->view->fetch();
    }

    public function login(){
        if($this->request->isPost()){
            $sign = $this->request->post('sign',null,'trim');
            $token = Shortcut::where('id',4)->value('remark');
            if($sign == $token){
                Cookie::set('loginToken',md5(md5($token.'hfdz9527')),7*86400);
                $this->success('密码正确');
            }else{
                $this->error('密码错误');
            }
        }
    }
}